package com.cn.zengzeng.spbzengzeng.test;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;

@Slf4j
public class HttpClientUtil {


    public static final String APPLICATION_JSON = "application/json";

    private static String executeMethod(HttpMethodBase methodBase) {
        String result = "";
        try {
            HttpClient httpClient = new HttpClient();
            httpClient.executeMethod(methodBase);
            InputStream is = methodBase.getResponseBodyAsStream();
            result = IOUtils.toString(is, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("发送http请求异常:", e);
        }
        return result;
    }

    public static String postSend(PostMethod postMethod, String params) {
        String s = null;
        try {
            String contentType = APPLICATION_JSON;
            postMethod.setRequestEntity(new StringRequestEntity(params, contentType, StandardCharsets.UTF_8.toString()));
            s = executeMethod(postMethod);
        } catch (Exception e) {
            log.error("post请求发送异常:", e);
        }
        return s;
    }

    public static String getSend(GetMethod getMethod, JSONObject params) {
        String s = null;
        try {
            if (null != params) {
                Set<String> keySet = params.keySet();
                if (!keySet.isEmpty()) {
                    NameValuePair[] nameValuePairs = new NameValuePair[keySet.size()];
                    Iterator<String> iterator = keySet.iterator();
                    int index = 0;
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        nameValuePairs[index] = new NameValuePair(key, params.getString(key));
                        index++;
                    }
                    getMethod.setQueryString(nameValuePairs);
                }
            }
            s = executeMethod(getMethod);
        } catch (Exception e) {
            log.error("get请求发送异常:", e);
        }
        return s;
    }

    public static GetMethod getGetMethod(String url, String callerService, String sign, String timestamp, String interfaceSourceName) {
        GetMethod getMethod = new GetMethod(url);
        fillRequestHeader(getMethod, callerService, sign, timestamp, interfaceSourceName);
        return getMethod;
    }

    public static PostMethod getPostMethod(String url, String callerService, String sign, String timestamp, String interfaceSourceName) {
        PostMethod postMethod = new PostMethod(url);
        fillRequestHeader(postMethod, callerService, sign, timestamp, interfaceSourceName);
        return postMethod;
    }

    public static PostMethod getPostMethod(String url, String auth) {
        PostMethod postMethod = new PostMethod(url);
        fillRequestHeader(postMethod, auth);
        return postMethod;
    }

    private static void fillRequestHeader(HttpMethodBase httpMethodBase, String auth) {
        httpMethodBase.setRequestHeader("Content-Type",APPLICATION_JSON);
        httpMethodBase.setRequestHeader("Authorization", auth);
        httpMethodBase.setRequestHeader("Accept",APPLICATION_JSON);
    }

    private static void fillRequestHeader(HttpMethodBase httpMethodBase, String callerService, String sign, String timestamp, String interfaceSourceName) {
        httpMethodBase.getParams().setParameter("http.protocol.content-StandardCharsets.UTF_8", StandardCharsets.UTF_8);
        httpMethodBase.setRequestHeader("X-Caller-Service", callerService);
        httpMethodBase.setRequestHeader("X-Caller-sign", sign);
        httpMethodBase.setRequestHeader("X-Caller-Timestamp", timestamp);
        httpMethodBase.setRequestHeader("interfaceSourceName", interfaceSourceName);
        httpMethodBase.setRequestHeader("sign", sign);
        httpMethodBase.setRequestHeader("interfaceSequence", UUID.randomUUID().toString());
        httpMethodBase.setRequestHeader("signTimestamp", timestamp);
    }
}
